package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import connection.Reader;

/**
 * Okno pro pozadavek o hru
 * 
 * @author dove
 * 
 */
public class SeekWin extends InternalFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JButton ok;

	private JButton canc;

	private JTextField time;

	private JTextField increment;

	private JCheckBox checkRated;

	private JComboBox colour;

	private JTextField from;

	private JTextField to;

	/**
	 * Nastavuje nazev okna
	 * 
	 * @param name
	 *            nazev okna
	 */
	public SeekWin(String name) {
		super(name);
	}

	public SeekWin getWindow() {

		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(10, 10, 10, 10);
		c.gridx = 0;
		c.gridy = 0;
		JLabel lTime = new JLabel(messages.getString("Time") + ":");
		pan.add(lTime, c);

		c.gridx = 1;
		time = new JTextField();
		time.setPreferredSize(new Dimension(50, 25));
		time.setText("0");
		pan.add(time, c);

		c.gridx = 0;
		c.gridy = 1;
		JLabel lIncre = new JLabel(messages.getString("Increment") + ":");
		pan.add(lIncre, c);

		c.gridx = 1;
		increment = new JTextField();
		increment.setPreferredSize(new Dimension(50, 25));
		increment.setText("0");
		pan.add(increment, c);

		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		JLabel lcolour = new JLabel(messages.getString("Color") + ":");
		pan.add(lcolour, c);

		c.gridx = 1;
		String[] colours = { "", "white", "black" };
		colour = new JComboBox(colours);
		colour.setSelectedIndex(0);
		pan.add(colour, c);

		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 2;
		checkRated = new JCheckBox(messages.getString("RatedGame") + ":");
		checkRated.setSelected(true);
		pan.add(checkRated, c);

		c.gridx = 0;
		c.gridy = 4;
		JPanel range = new JPanel();
		range.setBorder(new TitledBorder(messages.getString("Rating_range")
				+ ":"));
		range.setName("Rating range:");

		range.setLayout(new GridBagLayout());
		GridBagConstraints c1 = new GridBagConstraints();

		c1.insets = new Insets(10, 10, 10, 10);
		c1.gridx = 0;
		c1.gridy = 0;
		JLabel lfrom = new JLabel(messages.getString("From") + ":");
		range.add(lfrom, c1);

		c1.gridx = 1;
		from = new JTextField();
		from.setPreferredSize(new Dimension(50, 25));
		range.add(from, c1);

		c1.gridx = 0;
		c1.gridy = 1;
		JLabel lto = new JLabel(messages.getString("To") + ":");
		range.add(lto, c1);

		c1.gridx = 1;
		to = new JTextField();
		to.setPreferredSize(new Dimension(50, 25));
		range.add(to, c1);

		pan.add(range, c);

		c.gridx = 0;
		c.gridy = 5;
		c.gridwidth = 1;
		ok = new JButton(messages.getString("Ok"));
		ok.addActionListener(this);
		pan.add(ok, c);

		c.gridx = 1;
		canc = new JButton(messages.getString("Cancel"));
		canc.addActionListener(this);

		pan.add(canc, c);

		this.add(pan);

		this.setLocation(30, 30);
		this.pack();
		this.setPreferredSize(new Dimension(400, 400));
		return this;
	}

	public void actionPerformed(ActionEvent a) {
		if (a.getSource() == ok) {
			String command = "seek ";

			command += time.getText() + " ";
			command += increment.getText() + " ";
			if (!checkRated.isSelected()) {
				command += "unrated ";
			} else {
				command += "rated ";
			}
			command += colour.getSelectedItem() + " ";
			command += from.getText() + "-";
			command += to.getText();
			logger.debug("SEEK:" + command);
			Reader.sendString(command);
			this.setVisible(false);
		}
		if (a.getSource() == canc) {
			this.setVisible(false);
		}
	}
}
